package com.webchat.ugc.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.constants.ResourceBehaviorConstants;
import com.webchat.domain.vo.request.act.ResourceBehaviorRequestVO;
import com.webchat.rmi.act.IResourceBehaviorClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ResourceBehaviorService {

    @Autowired
    private IResourceBehaviorClient resourceBehaviorClient;

    /**
     * RPC 请求ACT服务,批量获取资源列表互动总量
     *
     * @param behaviorType
     * @param resourceType
     * @param resourceIds
     * @return
     */
    public Map<Long, Long> countBehavior(String behaviorType, String resourceType, List<Long> resourceIds) {

        ResourceBehaviorRequestVO resourceBehaviorRequest = new ResourceBehaviorRequestVO();
        resourceBehaviorRequest.setBehaviorType(behaviorType);
        resourceBehaviorRequest.setResourceType(resourceType);
        resourceBehaviorRequest.setResourceIds(resourceIds);
        APIResponseBean<Map<Long, Long>> responseBean = resourceBehaviorClient.countBehavior(resourceBehaviorRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyMap();
    }

    /**
     * 批量获取用户资源互动状态
     *
     * @param userId
     * @param behaviorType
     * @param resourceType
     * @param resourceIds
     * @return
     */
    public Map<Long, Boolean> isBehavior(String userId, String behaviorType, String resourceType, List<Long> resourceIds) {

        ResourceBehaviorRequestVO resourceBehaviorRequest = new ResourceBehaviorRequestVO();
        resourceBehaviorRequest.setUserId(userId);
        resourceBehaviorRequest.setBehaviorType(behaviorType);
        resourceBehaviorRequest.setResourceType(resourceType);
        resourceBehaviorRequest.setResourceIds(resourceIds);
        APIResponseBean<Map<Long, Boolean>> responseBean = resourceBehaviorClient.isBehavior(resourceBehaviorRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyMap();
    }


    /**
     * 批量查询资源互动人员列表
     *
     * @param behaviorType
     * @param resourceType
     * @param resourceIds
     * @param size
     * @return
     */
    public Map<String, Set<String>> listBehaviorUsers(String behaviorType, String resourceType, List<Long> resourceIds, int size) {

        ResourceBehaviorRequestVO resourceBehaviorRequest = new ResourceBehaviorRequestVO();
        resourceBehaviorRequest.setBehaviorType(behaviorType);
        resourceBehaviorRequest.setResourceType(resourceType);
        resourceBehaviorRequest.setResourceIds(resourceIds);
        resourceBehaviorRequest.setSize(size);
        APIResponseBean<Map<String, Set<String>>> responseBean = resourceBehaviorClient.listBehaviorUsers(resourceBehaviorRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyMap();
    }

}
